package com.translate.web.service.impl;

import com.translate.web.dao.CommissionManagementMapper;
import com.translate.web.param.contract.IWithdrawalAddParam;
import com.translate.entity.CommissionManagement;
import com.translate.vo.contract.ICommissionManagementVO;
import com.translate.web.service.ICommissionManagementService;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.CopyEntity;

import java.math.BigDecimal;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CommissionManagementServiceImpl extends BaseServiceImpl<CommissionManagement>
		implements ICommissionManagementService {
	public CommissionManagementServiceImpl(CommissionManagementMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(CommissionManagement data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(CommissionManagement data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<CommissionManagement> getEntityClass() {
		return CommissionManagement.class;
	}

	@Override
	public <VO extends ICommissionManagementVO> VO getDataByUserId(long userId, Class<VO> type) {

		CommissionManagement dataByUserId = getCommissonManagent(userId);

		if (dataByUserId == null) {
			try {
				return type.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				// TODO Auto-generated catch block
				throw new IllegalStateException("数据错误");
			}
		}

		return CopyEntity.copyEntity(dataByUserId, type);
	}

	public CommissionManagement getCommissonManagent(long userId) {
		CommissionManagementMapper baseMapper = this.getBaseMapper();

		return baseMapper.getDataByUserId(userId);
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public CommissionManagement getCommissonManagentForUpdate(long userId) {
		CommissionManagementMapper baseMapper = this.getBaseMapper();

		return baseMapper.getCommissonManagentForUpdate(userId);
	}

	@Override
	public int exchangeAcademicCoins(IWithdrawalAddParam addParam) {

		BigDecimal amount = addParam.getAmount();

		long userId = addParam.getUserId();
		CommissionManagement commissonManagent = this.getCommissonManagent(userId);

		BigDecimal subtract = commissonManagent.getWithdrawableAmount().subtract(amount);

		BigDecimal zero = new BigDecimal(0);

		if (subtract.compareTo(zero) < 0) {
			return -1;
		}

		CommissionManagement management = new CommissionManagement();

		management.setWithdrawableAmount(subtract);

		Integer id = commissonManagent.getId();
		management.setId(id);

		this.updateByPrimaryKeySelective(management);
		return id;
	}

	@Override
	@Transactional(propagation = Propagation.MANDATORY)
	public int insertRecommendUser(long userId, String userName) {

		CommissionManagement commissonManagentForUpdate = this.getCommissonManagentForUpdate(userId);

		if (null == commissonManagentForUpdate) {

			CommissionManagement management = new CommissionManagement();

			management.setUserId((int) userId);

			management.setUserName(userName);

			BigDecimal withdrawableAmount = new BigDecimal(0);
			management.setWithdrawableAmount(withdrawableAmount);

			long timeMillis = System.currentTimeMillis() / 1000L;
			management.setCreateTime(timeMillis);

			management.setUpdateTime(timeMillis);

			management.setFreezeAmount(withdrawableAmount);

			management.setRecommenNum(1);

			management.setTotalAmount(withdrawableAmount);

			return this.insert(management);
		}

		CommissionManagement commissionManagement = new CommissionManagement();
		
		commissionManagement.setUserName(userName);
		
		commissionManagement.setId(commissonManagentForUpdate.getId());
		
		commissionManagement.setRecommenNum(commissonManagentForUpdate.getRecommenNum() + 1);
		
		return this.updateByPrimaryKeySelective(commissionManagement);	

	}
}